package com.example.myapplication.fragment;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.myapplication.BaseFgm;
import com.example.myapplication.R;
import com.example.myapplication.adp.FragmentPagerAdp;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ClipPagerTitleView;

import java.util.ArrayList;

public class SquareChildFgm extends BaseFgm {

    private ArrayList<BaseFgm> fragments = new ArrayList<>();
    private CommonNavigator commonNavigator;
    private ArrayList<String> titles = new ArrayList<>();
    private ViewPager viewPager;
    private MagicIndicator tablayout;
    private FragmentPagerAdp pagerAdp;

    @Override
    protected void logicStart() {

    }

    @Override
    protected void initView() {
        viewPager = rootView.findViewById(R.id.viewPager);
        tablayout = rootView.findViewById(R.id.tabLayout);

        titles.add("书籍");
        titles.add("电子");
        titles.add("衣物");
        titles.add("日杂");

        fragments.add(MarketListFgm.getInstance(1));
        fragments.add(MarketListFgm.getInstance(2));
        fragments.add(MarketListFgm.getInstance(3));
        fragments.add(MarketListFgm.getInstance(4));


        commonNavigator = new CommonNavigator(getContext());

        pagerAdp = new FragmentPagerAdp(getChildFragmentManager(), fragments);

        viewPager.setAdapter(pagerAdp);

        commonNavigator.setAdapter(new CommonNavigatorAdapter() {

            @Override
            public int getCount() {
                return fragments.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, int index) {
                ClipPagerTitleView clipPagerTitleView = new ClipPagerTitleView(context);

                clipPagerTitleView.setText(titles.get(index));
                clipPagerTitleView.setTextColor(Color.parseColor("#333333"));
                clipPagerTitleView.setClipColor(Color.parseColor("#e88b00"));

                clipPagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem(index);
                    }
                });

                return clipPagerTitleView;
            }


            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator linePagerIndicator = new LinePagerIndicator(context);
                linePagerIndicator.setColors(Color.parseColor("#e88b00"));
                linePagerIndicator.setLineWidth(5);
                return linePagerIndicator;    // 没有指示器，因为title的指示作用已经很明显了
            }
        });
        commonNavigator.setAdjustMode(true);
        tablayout.setNavigator(commonNavigator);
//        tablayout.set
        ViewPagerHelper.bind(tablayout, viewPager);

    }

    @Override
    protected void setListener() {

    }

    @Override
    public void refreshState() {
        super.refreshState();
        for (BaseFgm fgm :fragments){
            fgm.refreshState();
        }

    }

    @Override
    protected int getlayout() {
        return R.layout.fgm_market;
    }
}
